package com.xinnet.mms.login.service.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.DigestsUtil;
import com.xinnet.core.utils.EncodesUtil;
import com.xinnet.mms.allot.dao.CustomAllotDao;
import com.xinnet.mms.dept.dao.DeptDao;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.dao.EmpDao;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.role.dao.RoleDao;

@Service("empService")
public class EmpServiceImpl implements EmpService {
	
	private static Logger logger = LoggerFactory.getLogger(EmpServiceImpl.class);
	
	public static final String HASH_ALGORITHM = "SHA-1";
	
	public static final int HASH_INTERATIONS = 1024;
	
	private static final int SALT_SIZE = 8;

	@Autowired
	private EmpDao loginDao;
	
	@Autowired
	private DeptDao deptDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private CustomAllotDao customAllotDao;
	@Autowired
	private EmpDao empDao;
	
	public void addEmp(Emp emp){
//		entryptPassword(emp);
		emp.setPassword(emp.getPlainPassword());
		loginDao.addEmp(emp);
	}
	
	
	@Override
	public Emp queryEmpByLoginName(String loginName) {
		if(null == loginName || "".equals(loginName)) {
			return null;
		}
		return this.loginDao.queryEmpByLoginName(loginName);
	}
	
	public List<Emp> getEmpList(Emp emp){
		List<Emp> empList = loginDao.getEmpList(emp);
		if(empList==null || empList.size()<=0){
			return null;
		}
		List<Role> roleList =   roleDao.getRoleList();
		List<Dept> deptList =  deptDao.getDeptList();
		for (Iterator<Emp> iterator = empList.iterator(); iterator.hasNext();) {
			Emp tempEmp = (Emp) iterator.next();
			String roleId = tempEmp.getRolId();
			if(roleId!=null && !"".equals(roleId)){
				if(roleList!=null && roleList.size()>0){
					for (Iterator<Role> iterator2 = roleList.iterator(); iterator2.hasNext();){
						Role role = (Role) iterator2.next();
						String tempRoleId = role.getId();
						if(roleId.equals(tempRoleId)){
							String roleName =  role.getName();
							tempEmp.setRoleName(roleName);
						}
					}
				}
			}
			String deptId = tempEmp.getId();
			if(deptId!=null && !"".equals(deptId)){
				if(deptList!=null && deptList.size()>0){
					for (Iterator<Dept> iterator3 = deptList.iterator(); iterator3.hasNext();){
						Dept dept = (Dept) iterator3.next();
						String tempDeptId = dept.getId();
						if(deptId.equals(tempDeptId)){
							String dname =  dept.getDname();
							tempEmp.setDname(dname);
						}	
					}
				}
			}	
		}
		return empList;
	}

	public String upateEmp(Emp emp){
		String flag="0";
		String loginName = emp.getLoginName();
		String empNo = emp.getEmpno();
		List<Emp> empList = loginDao.getEmpByLoginName(loginName);
		if(empList==null || empList.size()<=0){
			return flag;
		}
		for (Iterator<Emp> iterator = empList.iterator(); iterator.hasNext();) {
			Emp emp2 = (Emp) iterator.next();
			String tempEmpNo = emp2.getEmpno();
			if(tempEmpNo.equals(empNo)){
				flag="2";
				break;
			}
		}
		//有其它的用户使用此名字
		if(flag.equals("2")){
			return flag;
		}
		//没有其它的用户使用此名字
		loginDao.updateEmp(emp);
		flag="1";
		return flag;
	}
	
	public List<Emp> getAllEmpList(){
		List<Emp> empList = loginDao.getAllEmpList();
		if(empList==null || empList.size()<=0){
			return null;
		}
		List<Role> roleList =   roleDao.getRoleList();
		List<Dept> deptList =  deptDao.getDeptList();
		for (Iterator<Emp> iterator = empList.iterator(); iterator.hasNext();) {
			Emp emp = (Emp) iterator.next();
			String roleId = emp.getRolId();
			if(roleId!=null && !"".equals(roleId)){
				if(roleList!=null && roleList.size()>0){
					for (Iterator<Role> iterator2 = roleList.iterator(); iterator2.hasNext();){
						Role role = (Role) iterator2.next();
						String tempRoleId = role.getId();
						if(roleId.equals(tempRoleId)){
							String roleName =  role.getName();
							emp.setRoleName(roleName);
						}
					}
				}
			}
			String deptId = emp.getId();
			if(deptId!=null && !"".equals(deptId)){
				if(deptList!=null && deptList.size()>0){
					for (Iterator<Dept> iterator3 = deptList.iterator(); iterator3.hasNext();){
						Dept dept = (Dept) iterator3.next();
						String tempDeptId = dept.getId();
						if(deptId.equals(tempDeptId)){
							String dname =  dept.getDname();
							emp.setDname(dname);
						}	
					}
				}
			}	
		}
		return empList;
	}
	
	public List<Emp> getEmpListByDeptIdStr(String deptIdStr){
		List<Emp> empList = loginDao.getEmpListByDeptIdStr(deptIdStr);
		return empList;
	}
	public List<Emp> getEmpListByDeptIdStrOrNoDeptRoleId(String deptIdStr){
		List<Emp> empList = loginDao.getEmpListByDeptIdStrOrNoDeptRoleId(deptIdStr);
		return empList;
	}
	
	public List<Emp> getEmpSetApprovalListByDeptIdStr(String deptIdStr){
		List<Emp> empList = loginDao.getEmpSetApprovalListByDeptIdStr(deptIdStr);
		return empList;
	}
	
	public List<Emp> getEmpListExceptDeptLeader(String deptId){
		List<Emp> empList = loginDao.getEmpListExceptDeptLeader(deptId);
		return empList;
		
	}
	
	
	public void updateEmpAccount(Emp emp){
		if (StringUtils.isNotBlank(emp.getPlainPassword())){
			entryptPassword(emp);
		}
		loginDao.updateEmpAccount(emp);
	}
	
	public void updateEmpRole(Emp emp){
		loginDao.updateEmpRole(emp);
	}
	
	public void bathUpdateEmpRole(String empnoStr,String deptId,String roleId,String isResponsible){
		String[] empnoArray = empnoStr.split(",");
		for (int i = 0; i < empnoArray.length; i++) {
			String empno = empnoArray[i];
			Emp emp = new Emp();
			emp.setEmpno(empno);
			emp.setId(deptId);
			emp.setRolId(roleId);
			emp.setIsResponsible(Integer.parseInt(isResponsible));
			loginDao.updateEmpRole(emp);
		}	
	}
	
	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 SHA-1 hash
	 */
	private  static void entryptPassword(Emp emp) {
		byte[] salt = DigestsUtil.generateSalt(SALT_SIZE);
		String  saltStr= (String)EncodesUtil.encodeHex(salt);
		emp.setSalt(saltStr);
		byte[] hashPassword = DigestsUtil.sha1(emp.getPlainPassword().getBytes(), salt, HASH_INTERATIONS);
		String  hashPasswordStr= (String)EncodesUtil.encodeHex(hashPassword);
		emp.setPassword(hashPasswordStr);
	}
	
	public List<Emp> getEmpByDeptAndChildDept(String deptId){
		List<Emp> empList = loginDao.getEmpByDeptAndChildDept(deptId);
		return empList;
	}
	
	public List<Emp> getEmpListForChildDept(String deptId){
		List<Emp> empList = loginDao.getEmpListForChildDept(deptId);
		return empList;
	}
	
	public void updateEmp(Emp emp){
		loginDao.updateEmp(emp);
	}

	/**根据员工编号获取员工*/
	@Override
	public Emp getEmpbyEmpno(String empno) {
		return loginDao.getEmpbyEmpno(empno);
	}

	/**根据员工编号删除员工*/
	@Override
	public void delEmpByEmpno(String empno) {
		loginDao.delEmpByEmpno(empno);
	}
	/**
	 * 模糊查询
	 */
	public List<Emp> queryEmpList(Emp emp){

		List<Emp> empList = loginDao.queryEmpList(emp);
		if(empList==null || empList.size()<=0){
			return null;
		}
		List<Role> roleList =   roleDao.getRoleList();
		List<Dept> deptList =  deptDao.getDeptList();
		for (Iterator<Emp> iterator = empList.iterator(); iterator.hasNext();) {
			Emp tempEmp = (Emp) iterator.next();
			String roleId = tempEmp.getRolId();
			if(roleId!=null && !"".equals(roleId)){
				if(roleList!=null && roleList.size()>0){
					for (Iterator<Role> iterator2 = roleList.iterator(); iterator2.hasNext();){
						Role role = (Role) iterator2.next();
						String tempRoleId = role.getId();
						if(roleId.equals(tempRoleId)){
							String roleName =  role.getName();
							tempEmp.setRoleName(roleName);
						}
					}
				}
			}
			String deptId = tempEmp.getId();
			if(deptId!=null && !"".equals(deptId)){
				if(deptList!=null && deptList.size()>0){
					for (Iterator<Dept> iterator3 = deptList.iterator(); iterator3.hasNext();){
						Dept dept = (Dept) iterator3.next();
						String tempDeptId = dept.getId();
						if(deptId.equals(tempDeptId)){
							String dname =  dept.getDname();
							tempEmp.setDname(dname);
						}	
					}
				}
			}	
		}
		return empList;	
	}
	
	public List<Emp> getEmpListByParam(Emp emp){
		List<Emp> empList = loginDao.getEmpListByParam(emp);
		return empList;
	}
	
	public List<Emp> getEmpListByParamOrNoDeptRoleId(Emp emp){
		List<Emp> empList = loginDao.getEmpListByParamOrNoDeptRoleId(emp);
		return empList;
	}

	/**
	 * 密码加密修改员工密码
	 */
	@Override
	public void updateEmpByHyManagerMD5Salt(Emp emp) {
//		entryptPassword(emp);
		emp.setPassword(emp.getPlainPassword());
		updateEmp(emp);
	}
	
	/**
	 * 根据员工iD找到员工的分公司
	 */
	@Override
	public String getCompanyIdFromEmp(String empno) throws SQLException {
		Emp emp=customAllotDao.getEmpByEmpno(empno);
		Dept dept=customAllotDao.getDeptByDeptno(emp.getId());
		return dept.getCompanyIdStr();
	}
	
	/**
	 * 取得指定产品code对应部门的销售
	 */
	@Override
	public Emp getSpecialEmpByProductCode(String productCode) throws Exception {
		Emp emp=empDao.getSpecialEmpByProductCode(productCode);
		if(emp==null) {
			return null;
		}
		return emp;
	}


	/**
	 * 修改员工剩余推送个数
	 */
	@Override
	public void updateSurplusLimit(Emp emp) throws SQLException {
		empDao.updateSurplusLimit(emp);
	}

}
